package com.yuji.aacar.controller.changepassword;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.ImageView;

import com.yuji.aacar.R;
import com.yuji.aacar.base.BaseActivity;
import com.yuji.aacar.base.EventConst;
import com.yuji.aacar.base.UserCenter;
import com.yuji.aacar.bean.response.UpdatePersonalInfoResponse;
import com.yuji.aacar.core.cores.event.CoreEventCenter;
import com.yuji.aacar.core.cores.services.CoreServiceResponse;
import com.yuji.aacar.service.BaseServiceMediator;
import com.yuji.aacar.service.BaseServiceParams;
import com.yuji.aacar.tools.Tools;
import com.yuji.aacar.utils.UmengUtils;
import com.yuji.aacar.widgets.autolayout.AutoRelativeLayout;
import com.yuji.aacar.widgets.edittext.ChooseCancelEditText;
import com.yuji.aacar.widgets.textview.PingFangTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * <li>Package:com.yuji.aacar.controller.self</li>
 * <li>Author: lining </li>
 * <li>Date: 2018/3/6</li>
 * <li>Description:   </li>
 */

public class ChangePasswordActivity extends BaseActivity {


    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.back_image)
    AutoRelativeLayout backImage;
    @BindView(R.id.title_content)
    PingFangTextView titleContent;
    @BindView(R.id.back_login)
    PingFangTextView backLogin;
    @BindView(R.id.title_message_iv)
    ImageView titleMessageIv;
    @BindView(R.id.title_message_rl)
    AutoRelativeLayout titleMessageRl;
    @BindView(R.id.title_rl)
    AutoRelativeLayout titleRl;
    @BindView(R.id.original_password_et)
    ChooseCancelEditText originalPasswordEt;
    @BindView(R.id.change_password_new_et)
    ChooseCancelEditText changePasswordNewEt;
    @BindView(R.id.change_password_confirm_et)
    ChooseCancelEditText changePasswordConfirmEt;
    @BindView(R.id.change_password_btn)
    Button changePasswordBtn;

    private String password;
    private String newpassword;
    private String confirmpassword;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_change_password);
        ButterKnife.bind(this);
        initTitle(true, "修改密码", null);
        super.onCreate(savedInstanceState);
    }

    @OnClick(R.id.change_password_btn)
    public void onClick() {
        if (isLeagl()){
            requestParams = BaseServiceParams.updatePersonalInfo(password,confirmpassword,newpassword);
            doTask(BaseServiceMediator.CHANGE_PASSWORD);
        }
    }

    private boolean isLeagl() {
        password =originalPasswordEt.getText().toString();
        newpassword =changePasswordNewEt.getText().toString();
        confirmpassword = changePasswordConfirmEt.getText().toString();
        if (TextUtils.isEmpty(password)){
            showToast("请输入原始密码！");
            return false;
        } else if (TextUtils.isEmpty(newpassword)){
            showToast("请输入新密码！");
            return false;
        }else if (TextUtils.isEmpty(newpassword)){
            showToast("请确认新密码！");
            return false;
        } else if (!newpassword.contentEquals(confirmpassword)){
            showToast("二次密码输入不一致！");
            return false;
        } else if (!Tools.isAccountStandard(password)){
            showToast("密码为6~16位数字或英文！");
            return false;
        } else if(!Tools.isAccountStandard(newpassword)){
            showToast("密码为6~16位数字或英文！");
            return false;
        }else if(!Tools.isAccountStandard(confirmpassword)){
            showToast("密码为6~16位数字或英文！");
            return false;
        }
        return true;
    }

    @Override
    public void requestSuccess(CoreServiceResponse response) {
        super.requestSuccess(response);
        if (response.getRequestCode() == BaseServiceMediator.CHANGE_PASSWORD_CODE) {
            UpdatePersonalInfoResponse logOutResponse = (UpdatePersonalInfoResponse) response.getResponse();
            if (logOutResponse.isSuccess()){
                UmengUtils.onLogout();
                UserCenter.logout(ChangePasswordActivity.this);
                CoreEventCenter.postMessage(EventConst.LOGIN_OUT, "退出登录");
                //                presentController(SplashActivity.class);
                this.finish();
            }

        }
    }

    @Override
    public void requestFailed(CoreServiceResponse response) {
        super.requestFailed(response);
    }
}
